resource "alicloud_key_pair" "k8s_key_wlbc" {
  provider       = alicloud.wulanchabu
  key_pair_name  = "test-ack-key"
  public_key     = "${file("file/id_rsa.pub")}"
}

data "alicloud_instance_types" "k8s_instance_wlbc" {
  provider             = alicloud.wulanchabu 
  cpu_core_count       = local.cpu_core_count
  memory_size          = local.memory_size
  kubernetes_node_role = "Worker"
}

resource "alicloud_cs_managed_kubernetes" "k8s_manager_wlbc" {
  provider                      = alicloud.wulanchabu
  name                          = "tf-test-aliyun-wlbc"
  version                       = "1.18.8-aliyun.1"
  worker_vswitch_ids            = split(",", join(",", alicloud_vswitch.vswitches_wlbc.*.id))
  cluster_spec                  = local.cluster_spec
  is_enterprise_security_group  = true
  new_nat_gateway               = false
  pod_vswitch_ids               = split(",", join(",", alicloud_vswitch.vswitches_wlbc.*.id))
  service_cidr                  = local.service_cidr
  worker_instance_types         = [data.alicloud_instance_types.k8s_instance_wlbc.instance_types.0.id]
  install_cloud_monitor         = true
  enable_ssh                    = true
  deletion_protection           = true
  load_balancer_spec            = "slb.s1.small"
  slb_internet_enabled          = false
  
  dynamic "addons" {
      for_each = local.cluster_addons
      content {
        name          = lookup(addons.value, "name", local.cluster_addons)
        config        = lookup(addons.value, "config", local.cluster_addons)
        disabled      = lookup(addons.value, "disabled", local.cluster_addons)
      }
  }
  

  runtime = {
    name    = "docker"
    version = "19.03.5"
  }

  depends_on = [alicloud_snat_entry.k8s_snat_entry_wlbc]
}

resource "alicloud_cs_kubernetes_node_pool" "k8s_wlbc_asg_1" {
  provider                     = alicloud.wulanchabu
  cluster_id                   = alicloud_cs_managed_kubernetes.k8s_manager_wlbc.id
  name                         = "tf-ack-asg-1"
  vswitch_ids                  = split(",", join(",", alicloud_vswitch.vswitches_wlbc.*.id)) 
  instance_types               = [data.alicloud_instance_types.k8s_instance.instance_types.0.id]
  key_name                     = alicloud_key_pair.k8s_key_wlbc.key_name
  system_disk_size             = 120
  scaling_config {
    min_size     = 3 
    max_size     = 10
  }
}